<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <el-form
      :model="formData"
      ref="formRef"
      :rules="formRules"
      vloading="formLoading"
      label-width="auto"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目" prop="projectId">
            <el-select v-model="formData.projectId" placeholder="请输入项目">
              <el-option
                v-for="dict in projectList"
                :key="dict.id"
                :label="dict.name"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="版本号" prop="version">
            <el-input v-model="formData.version" placeholder="请输入版本号" :clearable="true" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="版本内容" prop="versionContent">
            <el-input
              v-model="formData.versionContent"
              placeholder="请输入版本内容"
              :clearable="true"
              type="textarea"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="formData.remark"
              placeholder="请输入备注"
              :clearable="true"
              type="textarea"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button type="primary" :disabled="formLoading" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup name="DevopsProjectVersionEdit" lang="ts">
import * as DevopsProjectVersionApi from '@/api/infra/devops/projectVersion'
import * as DevopsProjectApi from '@/api/infra/devops/project'
const projectList = ref()
let formType = ''
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const dialogTitle = ref('')
const dialogVisible = ref(false)
const formRef = ref()
const formLoading = ref(false)
const formData = ref({
  id: undefined,
  projectId: undefined,
  version: undefined,
  versionContent: undefined,
  remark: undefined
})
const formRules = reactive({})
/** 打开弹窗 */
const open = async (type, id) => {
  projectList.value = await DevopsProjectApi.listQuery()
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  resetForm()
  formType = type
  if (type == 'update') {
    formData.value = await DevopsProjectVersionApi.singleGet(id)
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    projectId: undefined,
    version: undefined,
    versionContent: undefined,
    remark: undefined
  }
  formRef.value?.resetFields()
}

const emit = defineEmits(['success'])
/** 提交表单 */
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return

  // 提交请求
  formLoading.value = true
  try {
    if (formType === 'create') {
      await DevopsProjectVersionApi.create(formData.value)
      message.success(t('common.createSuccess'))
    } else {
      await DevopsProjectVersionApi.update(formData.value)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script>
